#include "qrunningcore.h"

#include "qrunninghost.h"
#include "qpageloader.h"

#include "../sharedLib/qlanguagemanager.h"
#include "../sharedLib/qusermanager.h"
#include "../sharedLib/qpagemanager.h"
#include "../sharedLib/qmemerymanager.h"
#include "../sharedLib/qabstractproperty.h"
#include "../sharedLib/xml.h"

#include <QFile>
#include <QXmlStreamReader>
#include <QDir>
#include <QApplication>
#include <QDesktopWidget>

using namespace RUNNINGLIB;

QRunningWindow::QRunningWindow(const QString &projectPath, QWidget *parent) :
    QMainWindow(parent),
    m_languageManager(new SHAREDLIB::QLanguageManager),
    m_userManager(new SHAREDLIB::QUserManager),
    m_pageManager(new SHAREDLIB::QPageManager),
    m_memeryManager(new SHAREDLIB::QMemeryManager),
    m_host(new QRunningHost(this)),
    m_projectPath(projectPath)
{
    setCentralWidget(m_host->getBaseWidget());
    m_host->setRunningWindow(this);
}

QRunningWindow::~QRunningWindow()
{
}

bool QRunningWindow::load()
{
    if(!open(m_projectPath))
    {
        return false;
    }
    m_languageManager->load(m_projectPath);
    m_userManager->load(m_projectPath);
    m_memeryManager->load(m_projectPath);
    m_pageManager->load(m_projectPath);
    m_host->setLanguageManager(m_languageManager);
    m_host->setMemeryManager(m_memeryManager);
    QString name=m_host->getPropertyValue("start_language").toString();
    QObject* obj=m_languageManager->getLanguage(name);
    if(obj==NULL)
    {
        obj=m_languageManager->getLanguage(0);
    }
    m_languageManager->setCurrentLanguage(obj);

    name=m_host->getPropertyValue("start_user").toString();
    obj=m_userManager->getUser(name);
    if(obj==NULL)
    {
        obj=m_userManager->getUser(0);
    }
    m_userManager->setCurrentUser(obj);

    m_host->setPagemanager(m_pageManager);
    m_host->setUsermanager(m_userManager);
    return true;
}

void QRunningWindow::clear()
{
    delete m_host;

    m_host=NULL;


    delete m_languageManager;
    m_languageManager=NULL;

    delete m_userManager;
    m_userManager=NULL;

    delete m_pageManager;
    m_pageManager=NULL;

    delete m_memeryManager;
    m_memeryManager=NULL;
}


bool QRunningWindow::open(const QString &filePath)
{
    if(filePath=="")
    {
        return false;
    }
    QString file=filePath+"/config.pro";
    QFile f(file);

    if(f.open(QFile::ReadOnly))
    {
        QXmlStreamReader r(&f);

        while(!r.atEnd() && !r.hasError())
        {
            if(r.readNext()==QXmlStreamReader::StartElement)
            {
                if(r.name().toString()=="Object")
                {
                    m_projectPath=filePath;
                    m_host->setPropertyValue("path",m_projectPath);
                    loadProject(&r);

                }
            }
        }
        f.close();
        if(r.hasError())
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    else
    {
        return false;
    }
}

void QRunningWindow::loadProject(QXmlStreamReader *r)
{
    SHAREDLIB::XMLObject  xml;

    xml.read(r);
    if(r->hasError())
    {
        return;
    }

    QList<SHAREDLIB::QAbstractProperty*>   pros=xml.propertys();
    SHAREDLIB::QAbstractProperty *temp;
    foreach(SHAREDLIB::QAbstractProperty* pro,pros)
    {
        temp=m_host->getProperty(pro->attribute("name").toString());
        if(temp!=NULL)
        {
            *temp=*pro;
        }
    }
}

bool QRunningWindow::run()
{
    if(!load())
    {
        qWarning("Project load failed!");
        return false;
    }
    m_host->firstScript();
    QSize desktop=qApp->desktop()->size();
    QSize size=this->size();
    move((desktop.width()-size.width())/2,(desktop.height()-size.height())/2-30);

    bool frame=m_host->getPropertyValue("frame").toBool();

    Qt::WindowFlags flags=windowFlags();
    if(!frame)
    {
        flags=Qt::Window | Qt::FramelessWindowHint;
    }
    this->setWindowFlags(flags);

    bool fullscreen=m_host->getPropertyValue("fullscreen").toBool();
    if(fullscreen)
    {
        this->showFullScreen();
    }

    QString firstPage=m_host->getPropertyValue("start_page").toString();

    if(firstPage=="")
    {
        return false;
    }

    if(!m_host->getPropertyValue("resizeabled").toBool())
    {
        this->setFixedSize(size);
    }

    show();
    raise();
    if(!m_host->showPage(firstPage))
    {
        this->close();
        return false;
    }
    emit runSignal();
    return true;
}

void QRunningWindow::stop()
{
    m_host->lastScript();
    clear();
    emit stopSignal();
}

void QRunningWindow::closeEvent(QCloseEvent *e)
{
    QMainWindow::closeEvent(e);
    stop();
}
